package com.music.common;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.music.entity.req.CmVipReq;
import com.music.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @author 71033
 */
@Component
public class AlipayUtils {

    @Autowired
    RedisUtils redisUtils;

    public String pay (HttpServletRequest httpRequest, HttpServletResponse httpResponse, CmVipReq cmVipReq) throws AlipayApiException {
        /**
         * 1.要去支付包请求的地址
         */

        //获得初始化的AlipayClient
        AlipayClient alipayClient =  new  DefaultAlipayClient( "https://openapi.alipaydev.com/gateway.do" , "2021000116660025", "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCKX7dgkb8nWg0/fdM2PrBePO1ux0+vmBe1SFO6nyAceIlsojgIkuVCMfvZ83uf7rN8iGnR3BhxBmz8H3ChPdNoXE3XpBPUC0lA5y1FUZFyCj7xC9C3OK5/FSq8bTIRHrKPXb3C9Gubdkub15jJwYxmEIJzk29rBkojyZYVaIYCvLaf9LhcFFSc0/z9K5tqI84pJqA3M1tJ/DTO+kPQDN0mntrVFvQ9OutEbkSvOGgzpg7/HiD9v12i97d0DTMOmrIycb66a1sxBrC4kq2hjkJhREa+Ms6+UpXgf+uppwS0fDkGWUy+cQZBvYFEXAwgJPSDnmAwMtQ0YABFf/tkpF3XAgMBAAECggEAN1duvPWtkOyILeOB7MkHurj9cPfP2/elS8CglBv+OMTQmZ0LcgpmHKJnCtVBlUZfjKf2QpOXRuT+A5wDbMmQWsXH8D3UHFNjOcz7oySrWgPVBozmLdFCoNrzW6cEhipT2djxAx7ERsn6FVsV5WM+ZrelpNnLju9WCZgfKXLDXiZEyrzgLe1uTbnOfP6h4wOR+Vr2ddy8zwZ0lggaqklSxkQ2l0A1c97Plg028g64D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ZdTmume0gLa6LrjjECANkpdiEwmFEOwUCgESJbp3Urh4oMNVf6h2UV0adltCgq7An9a0+GC41neglGzblvV9bmrS6jV3r/SuOuZFJMAN6lkY+9tFjzoPfdCYGgSiyrXw6hz2wOeWzgn6An82cbfqR6pHOno1APvFc6TGI0n1kck1993ZrXuDEyLdUUNE6j5tV9Pctw5xKfMlvINPoSgmZoDKFljdPfaTK2lBy+/jXsqO5qjDbKpZk/jDc03GMiBxpeeAX4L83V0W0dJbqB9odSA3xM75JaEBX0jkGeQIDAQAB", "RSA2");
        //创建API对应的request
        AlipayTradePagePayRequest alipayRequest =  new  AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        alipayRequest.setReturnUrl( "http://zjds.ngrok2.xiaomiqiu.cn/#/index01" );
        alipayRequest.setNotifyUrl( "http://9bgi6c.natappfree.cc/PayController/returnUrl" );
//        System.out.println(cmVipReq);
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        String replace = s.replace("-", "");

        cmVipReq.setUuid(replace);
        System.out.println(cmVipReq);
//        RedisUtils redisUtils = new RedisUtils();
        redisUtils.set(cmVipReq.getUuid(),cmVipReq,60);
        //生成订单信息存储到数据库，默认的订单信息时未支付
        alipayRequest.setBizContent( "{"  +
                "    \"out_trade_no\":\""+replace+"\","  +
                "    \"buyer_id\":\""+cmVipReq.getuId()+"\","  +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\","  +
                "    \"total_amount\":\""+cmVipReq.getMoney()+"\","  +
                "    \"subject\":\""+cmVipReq.getName()+"\","  +
                "    \"body\":\""+cmVipReq.getDesc()+"\","  +
                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\","  +
                "    \"extend_params\":{"  +
                "    \"sys_service_provider_id\":\"2088511833207846\""  +
                "    }" +
                "  }" ); //填充业务参数
        String form= "" ;
        try  {
            //调用SDK生成表单
            form = alipayClient.pageExecute(alipayRequest).getBody();
//            System.out.println(form);
        }  catch  (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType( "text/html;charset=UTF-8");
        return form;
    }
}
